/**
 * futures : masterlink.futures.vo.RepositoryGreeks.java
 */
package masterlink.futures.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import baytony.util.Util;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/11/10
 * @version 1.0.0	
 */
public class RepositoryGreeksA implements Serializable, RepositoryGreeks {
	
	private static final long serialVersionUID = -2228101956094549088L;
	private final Account account;
	private final List<GreeksRiskManager> managers=new ArrayList<GreeksRiskManager>();
	private Long qty=null;
	private Double avgprice=null;
	private Double delta=null;
	private Double gamma=null;
	private Double theta=null;
	private Double vega=null;
	private Double rho=null;
	private Double timeprice=null;
	private Double balance=null;
	private Double dealprice=null;
	
	/**
	 * @param account
	 * @param manager
	 */
	public RepositoryGreeksA(Account account) {
		this.account = account;
	}
	
	public void addGreeksRiskManager(GreeksRiskManager manager){
		if(manager!=null){
			managers.add(manager);
		}
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getBroker()
	 */
	public String getBroker(){
		if(account!=null){
			return account.getId().getBroker();
		}
		return "";
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getAccount()
	 */
	public String getAccount(){
		if(account!=null){
			return account.getId().getAcno();
		}
		return "";
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getAccountName()
	 */
	public String getAccountName(){
		if(account!=null){
			return account.getName();
		}
		return "";
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getManagers()
	 */
	@Override
	public List<GreeksRiskManager> getManagers(){
		return managers;
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getQty()
	 */
	@Override
	public Long getQty() {
		if(!Util.isEmptyList(managers)){
			if(qty==null){
				qty=0l;
				for(GreeksRiskManager m:managers){
					qty+=Math.abs(m.getId().getQty());
				}
			}
			return qty;
		}
		return 0l;
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getAvgprice()
	 */
	@Override
	public Double getAvgprice() {
		if(!Util.isEmptyList(managers)){
			if(avgprice==null){
				avgprice=0d;
				for(GreeksRiskManager m:managers){
					avgprice+=m.getId().getAvgprice();
				}
			}
			return avgprice;
		}
		return 0d;
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getDelta()
	 */
	@Override
	public Double getDelta() {
		if(!Util.isEmptyList(managers)){
			if(delta==null){
				delta=0d;
				for(GreeksRiskManager m:managers){
					delta+=(m.getDelta());
				}
			}
			return delta;
		}
		return 0d;
	}

	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getGamma()
	 */
	@Override
	public Double getGamma() {
		if(!Util.isEmptyList(managers)){
			if(gamma==null){
				gamma=0d;
				for(GreeksRiskManager m:managers){
					gamma+=m.getGamma();
				}
			}
			return gamma;
		}
		return 0d;
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getTheta()
	 */
	@Override
	public Double getTheta() {
		if(!Util.isEmptyList(managers)){
			if(theta==null){
				theta=0d;
				for(GreeksRiskManager m:managers){
					theta+=m.getTheta();
				}
			}
			return theta;
		}
		return 0d;
	}

	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getVega()
	 */
	@Override
	public Double getVega() {
		if(!Util.isEmptyList(managers)){
			if(vega==null){
				vega=0d;
				for(GreeksRiskManager m:managers){
					vega+=m.getVega();
				}
			}
			return vega;
		}
		return 0d;
	}

	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getRho()
	 */
	@Override
	public Double getRho() {
		if(!Util.isEmptyList(managers)){
			if(rho==null){
				rho=0d;
				for(GreeksRiskManager m:managers){
					rho+=m.getRho();
				}
			}
			return rho;
		}
		return 0d;
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getTimeprice()
	 */
	@Override
	public Double getTimeprice() {
		if(!Util.isEmptyList(managers)){
			if(timeprice==null){
				timeprice=0d;
				for(GreeksRiskManager m:managers){
					timeprice+=(m.getId().getTimeprice());
				}
			}
			return timeprice;
		}
		return 0d;
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getBalance()
	 */
	@Override
	public Double getBalance() {
		if(!Util.isEmptyList(managers)){
			if(balance==null){
				balance=0d;
				for(GreeksRiskManager m:managers){
					balance+=(m.getId().getBalance());
				}
			}
			return balance;
		}
		return 0d;
	}
	
	/* (non-Javadoc)
	 * @see masterlink.futures.vo.RepositoryGreeks#getDealprice()
	 */
	@Override
	public Double getDealprice() {
		if(!Util.isEmptyList(managers)){
			if(dealprice==null){
				dealprice=0d;
				for(GreeksRiskManager m:managers){
					dealprice+=(m.getId().getDealprice());
				}
			}
			return dealprice;
		}
		return 0d;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("RepositoryGreeks [acc=");
		builder.append(account);
		builder.append(", manager=");
		builder.append(managers.size());
		builder.append("]");
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((account == null) ? 0 : account.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof RepositoryGreeksA))
			return false;
		RepositoryGreeksA other = (RepositoryGreeksA) obj;
		if (account == null) {
			if (other.account != null)
				return false;
		} else if (!account.equals(other.account))
			return false;
		return true;
	}
}
